<template>
  <section>
    <!--工具条(查询)-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="search">
        <el-form-item>
          <el-input v-model="search.key" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="100">
      </el-table-column>
      <el-table-column type="index" width="100">
      </el-table-column>
      <el-table-column prop="name" label="名字" width="240">
      </el-table-column>
      <el-table-column prop="sn" label="标识" width="240">
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleSave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="danger" size="small" @click="handlePermission(scope.$index, scope.row)">设置权限</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条(批量删除和分页)-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total, prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="10"
          :total="pageInfo.total"
          :current-page="search.currentPage"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="100px" ref="saveForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色唯一标识" prop="name">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit">提交</el-button>
      </div>
    </el-dialog>

    <!--设置权限界面-->
    <el-dialog title="设置权限" :visible.sync="roleFormVisible" :close-on-click-modal="false">
      <el-form :model="roleForm" label-width="80px" ref="rolePermissionForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.role.name" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="授权" prop="name">
          <el-tree
              :props="props"
              :data="data"
              default-expand-all
              node-key="id"
              ref="perTree"
              show-checkbox>
          </el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="roleFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addAuth">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      search: {
        key: '',
        pageSize: 5,
        currentPage: 1
      },
      pageInfo: {
        data: [],
        total: 0
      },
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,
      saveForm: {
        id: null,
        name: "",
        sn: ""
      },
      roleFormVisible: false,
      roleForm: {
        role: {
          id: null,
          name: "",
          sn: ""
        },
        permissionList: []
      },
      props: {
        label: 'name',
        children: 'children'
      },
      data: []
    }
  },
  methods: {
    //查询 通过角色id查询中间表的sn，再通过sn去查权限表的id
    queryAuthByRoleId() {
      this.$http.post("/role/queryAuth/" + this.roleForm.role.id)
          .then(res => {
            res = res.data;
            if (res.success) {
              var ids = res.data;
              this.$refs.perTree.setCheckedKeys(ids);
            }
          })
          .catch(res => {
          })
    },
    //查看权限树
    getPerTree() {
      this.$http.get("/permission/perTree")
          .then(res => {
            res = res.data;
            if (res.success) {
              this.data = res.data
              //回显数据
              // this.$refs.perTree.setCheckedKeys([1585,1586]);
              this.queryAuthByRoleId();
            }
          })
          .catch(res => {
          })
    },
    //获取角色例表
    getRoleS() {
      this.$http.post("/role", this.search)
          .then(res => {
            res = res.data;
            if (res.success) {
              this.pageInfo = res.data;
            }
          })
          .catch(res => {
          })
    },
    //点击新增
    handleAdd() {
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        name: "",
        sn: ""
      };
    },
    //当每页条数发生变化
    handleSizeChange(val) {
      this.search.pageSize = val;
      this.getRoleS();
    },
    //当前页发生变化
    handleCurrentChange(val) {
      this.search.currentPage = val;
      this.getRoleS();
    },
    //点击查询
    queryData() {
      this.search.currentPage = 1;
      this.getRoleS();
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/role/" + row.id)
            .then(res => {
              res = res.data;
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "删除成功"
                })
              } else {
                this.$message.error("删除失败" + res.msg)
              }
              this.listLoading = false;
              this.queryData();
            })
            .catch(res => {
            });
      }).catch(() => {

      });
    },
    //点击显示新增/修改界面
    handleSave: function (index, row) {
      this.saveFormVisible = true;
      this.saveForm = row;
      console.log(row);
      //调用查询角色
      this.getRoleS()
    },
    //点击显示设置权限界面
    handlePermission(index, row) {
      this.roleFormVisible = true;
      this.roleForm.role = row;
      this.getPerTree();
    },

    //新增/修改
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {})
              .then(() => {
                //NProgress.start();
                let para = Object.assign({}, this.saveForm);
                this.$http.put("/role", para)
                    .then(res => {
                      this.saveFormVisible = false;
                      this.$message({type: "success", message: "保存成功"});
                      this.queryData();
                    })
                    .catch(res => {
                    });
              });
        }
      });
    },

    //保存权限设置
    addAuth() {
      this.roleForm.permissionList = this.$refs.perTree.getCheckedNodes();
      this.$http.post("/role/roleAuth", this.roleForm)
          .then(res => {
            res = res.data;
            if (res.success) {
              this.$message({type: "success", message: "授权成功"});
            }
          })
          .catch(res => {
          })
      this.roleFormVisible = false;
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {type: 'warning'})
          .then(() => {
            this.listLoading = true;
            this.$http.patch("/role", ids)
                .then(res => {
                  res = res.data;
                  if (res.success) {
                    this.$message({type: "success", message: "删除成功"});
                  } else {
                    this.$message.error("删除失败" + res.msg);
                  }
                  this.listLoading = false;
                  this.queryData();
                })
                .catch(res => {
                });
          })
          .catch(() => {});
    }
  },
  mounted() {
    this.getRoleS();
  }
}

</script>

<style scoped>

</style>